﻿using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NewRetail.Application.Components.Goods.Entity;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Application.Components.Warehouse
{
    public class WarehouseConfiguration
    {
        public Warehouse.Entity.Warehouse Warehouse { get; set; }
        public GoodsCategory[] goodsCategories { get; set; }
        public PosCashier[] posCashiers { get; set; }
        public PosCashRegister[] posCashRegisters { get; set; }
        public PosMemberType[] PosMemberTypes { get; set; }

        public PosIntegral[] PosIntegrals { get; set; }
        public PosIntegralRule[] PosIntegralRules { get; set; }
        public PromoteSalesFullCut[] PromoteSalesFullCuts { get; set; }
        public PromoteSalesDiscount[] PromoteSalesDiscounts { get; set; }
        public PromoteSalesDiscountCategory[] PromoteSalesDiscountCategories { get; set; }
        public PromoteSalesDiscountGoods[] PromoteSalesDiscountGoodss { get; set; }
        /// <summary>
        /// 会员促销规则，生日双倍积分，88折等
        /// </summary>
        public PosMemberPromotionRule[] PosMemberPromotionRules { get; set; }
    }

    public interface IWarehouseService : IApplicationService
    {
        Task<WarehouseConfiguration> GetWarehouseConfiguration(string warehouseId);
    }


    public class WarehouseService : MyApplicationService, IWarehouseService
    {
        private readonly ILogger logger;
        private readonly IRepository<Warehouse.Entity.Warehouse> warehouses;

        public WarehouseService(
            ILogger<WarehouseService> logger,
            IRepository<Warehouse.Entity.Warehouse> warehouses)
        {
            this.logger = logger;
            this.warehouses = warehouses;
        }

        public async Task<WarehouseConfiguration> GetWarehouseConfiguration(string warehouseId)
        {
            warehouseId.Should().NotBeNullOrEmpty();

            var db = await this.warehouses.GetDbContextAsync();

            var data = new WarehouseConfiguration();

            data.Warehouse = await db.Set<Warehouse.Entity.Warehouse>().IgnoreQueryFilters().AsNoTracking()
                .FirstOrDefaultAsync(x => x.Id == warehouseId);

            data.goodsCategories = await db.Set<GoodsCategory>().IgnoreQueryFilters().AsNoTracking().Where(x => x.warehouseID == warehouseId)
                .MaxTake().ToArrayAsync();

            data.posCashiers = await db.Set<PosCashier>().IgnoreQueryFilters().AsNoTracking()
                .Where(x => x.warehouseID == warehouseId).MaxTake().ToArrayAsync();

            data.posCashRegisters = await db.Set<PosCashRegister>().IgnoreQueryFilters().AsNoTracking()
                .Where(x => x.warehouseID == warehouseId).MaxTake().ToArrayAsync();

            data.PosMemberTypes = await db.Set<PosMemberType>().IgnoreQueryFilters().AsNoTracking().MaxTake().ToArrayAsync();

            data.PosIntegralRules = await db.Set<PosIntegralRule>().IgnoreQueryFilters().AsNoTracking().MaxTake().ToArrayAsync();

            data.PosIntegrals = await db.Set<PosIntegral>().IgnoreQueryFilters().AsNoTracking().MaxTake().ToArrayAsync();

            data.PromoteSalesDiscountCategories = await db.Set<PromoteSalesDiscountCategory>().IgnoreQueryFilters().AsNoTracking()
                .MaxTake().ToArrayAsync();

            data.PromoteSalesDiscountGoodss = await db.Set<PromoteSalesDiscountGoods>().IgnoreQueryFilters().AsNoTracking()
                .MaxTake().ToArrayAsync();

            data.PromoteSalesFullCuts = await db.Set<PromoteSalesFullCut>().IgnoreQueryFilters().AsNoTracking()
                .Where(x => x.warehouseID == warehouseId).MaxTake().ToArrayAsync();

            data.PromoteSalesDiscounts = await db.Set<PromoteSalesDiscount>().IgnoreQueryFilters().AsNoTracking()
                .Where(x => x.warehouseID == warehouseId).MaxTake().ToArrayAsync();

            data.PosMemberPromotionRules = await db.Set<PosMemberPromotionRule>().IgnoreQueryFilters().AsNoTracking().MaxTake().ToArrayAsync();

            this.logger.LogInformation($"{warehouseId}-同步信息");
            return data;
        }
    }
}
